import Cookies from 'js-cookie';
import { getDataInfor} from 'api/reportForms';
import moment from 'moment';

const app = {
    state: {
        table: {
            pageSize: +Cookies.get('pageSize') || 10,
            pageSizes: [10, 25, 50]
        }
    },
    mutations: {
        CHANGE_PAGE_SIZE: (state, pageSize) => {
            Cookies.set('pageSize', pageSize);
            state.table.pageSize = pageSize;
        }
    },
    actions: {
        ChangePageSize: ({ commit }, pageSize) => {
            commit('CHANGE_PAGE_SIZE', pageSize)
        },
                // 导出
        // exportFile({ commit }, fileInfor) {
        //     return new Promise((resolve, reject) => {
        //         let query_id=fileInfor.query_id,
        //             show_list=fileInfor.show_list,
        //             real_time=fileInfor.real_time,
        //             file_name=fileInfor.file_name,
        //             field_name=fileInfor.field_name,
        //             type=fileInfor.type,
        //             query= fileInfor.query;
        //         exportTable(query_id, show_list, real_time, file_name, field_name, type, query).then(res => {
        //             resolve(res)
        //         }).catch(error => {
        //             reject(error);
        //         });
                     
        //     });
        // },
         // 调接口
        getQuery({ commit }, inforSent) {
            return new Promise((resolve, reject) => {
                let queryId=inforSent.query_id,
                    realTime=inforSent.real_time,
                    query=inforSent.query;
                var json = {};
                let showList = false;
                let type = inforSent.configWay;
                getDataInfor(queryId, realTime, query, showList, type).then(res => {
                    let xAxisType=query.unit; // 时间单位
                    let rows = null; // 列表数据
                    let series = null; // x轴
                    let dataList = [];// 数据(line折线的)
                    let barList = []; // 柱状图
                    inforSent.pieInnerList = inforSent.pieInnerList || [];
                    inforSent.pieOuterList = inforSent.pieOuterList || [];
                    inforSent.pieList = inforSent.pieList || [];
                    let legendList = [];// 标题
                    let xAxisData=[];// x轴

                    if (type == 'retention') {
                        rows=res.data.content.rows;
                        rows.forEach((val, i) => {
                            let legend = '第'+(i+1)+'日';
                            // line线性图 
                            if (inforSent.type=='line') {
                                dataList.push({// 数据
                                    // name: val.by_value,
                                    name: legend,
                                    type: 'line',
                                    smooth: true,
                                    itemStyle: {normal: {areaStyle: {type: 'default'}}},
                                    stack: inforSent.type,
                                    data: []
                                });
                            }
                            legendList.push(legend);
                            xAxisData.push(val.by_value);
                        });
                        dataList.forEach((m, i) => {
                            rows.forEach((n, j) => {
                                m.data.push(n.cells[i] ? n.cells[i].percent : 0);
                            });
                        });
                    } else {
                        if (type == 'user') {
                            rows=res.data.content.rows;
                            series=res.data.content.series;
                        } else {
                            rows=res.data.content.detail_result.rows;
                            series=res.data.content.detail_result.series;
                        }
  
                        rows.forEach((val) => {
                            let values=[];
                            val.values.forEach((vals) => {
                                values.push(vals[0])
                            });
                            // 数据（根据line线性图   还是bar柱状图  还是pie饼状图 获取数据）
                            
                            // pie饼状图
                            if (inforSent.type=='pie') {
                               
                                let total = 0;
                                values.forEach((item) => {
                                    total=total+item;
                                });
                                // 两个环的饼图
                                if (inforSent.innerOrOut=='inner') {
                                    inforSent.pieInnerList.push({// 内环
                                        name: val.by_values[0] || '[未知]',
                                        value: total
                                    });
                                } else if (inforSent.innerOrOut=='outer') {
                                    inforSent.pieOuterList.push({// 外环
                                        name: val.by_values[0] || '[未知]',
                                        value: total
                                    });
                                } else { // 一个环的饼图
                                    inforSent.pieList.push({
                                        name: val.by_values[0] || '[未知]',
                                        value: total
                                    });
                                }
                            }
                            // line线性图 
                            if (inforSent.type=='line') {
                                console.log(inforSent, '888')
                                dataList.push({// 数据
                                    name: val.by_values[0] || inforSent.dataListName||'[未知]',
                                    type: 'line',
                                    stack: inforSent.type,
                                    data: values
                                });
                            }
                            // bar柱状图
                            if (inforSent.type=='bar') {
                                barList.push({// 数据
                                    name: val.by_values[0],
                                    type: 'bar',
                                    stack: '总量',
                                    itemStyle: {
                                        normal: {
                                            barBorderRadius: inforSent.barBorderRadius?inforSent.barBorderRadius:0// 柱状图的圆角(不传圆角就默认0，就是不加圆角的意思)
                                        },
                                        emphasis: {
                                            barBorderRadius: inforSent.barBorderRadius?inforSent.barBorderRadius:0// 柱状图的圆角(不传圆角就默认0，就是不加圆角的意思)
                                        }
                                    },
                                    data: values
                                });
                            }
                            
                            // 标题，undefinned的想得到未知
                            if (inforSent.dontKnow) {
                                legendList.push(val.by_values[0]?val.by_values[0]:'[未知]')
                                return
                            }
                            legendList.push(val.by_values[0] || inforSent.legend); // 标题（每种图表都是这样获取）
                        });
                       
                        series.forEach((val) => {
                            if (xAxisType) {// 查询条件里面的 近30天（formInline.time默认空） 还是近12个月
                                
                                if (xAxisType== 'day') {
                                    let item = moment(val).format('MM-DD');// 取月-日
                                    xAxisData.push(item);
                                } else if (xAxisType == 'hour') {
                                    let item = moment(val).format('HH');
                                    xAxisData.push(item);
                                } else if (xAxisType == 'month') {// 只取月
                                    let item = moment(val).format('MM')+'月';
                                    xAxisData.push(item);
                                }
                            }
                        });
                    }

                    json.dataList = dataList;// 折现的数据
                    json.legendList = legendList;// 标题
                    json.xAxisData = xAxisData;// x轴坐标
                    json.pieInnerList = inforSent.pieInnerList || [];// 饼图的内环
                    json.pieOuterList = inforSent.pieOuterList || [];// 饼图的外环
                    json.pieList = inforSent.pieList || [];// 一个环的饼图
                    json.barList = barList; // 柱状图的数据
                    json.data = res.data.content;
                    resolve(json);
                }).catch(error => {
                    reject(error);
                });
            });
        }
    }
};

export default app;
